home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1249 / roman.t < prev    next >
Text File  |  1997-04-18  |  2KB  |  100 lines

  1. %
  2. % "roman.t" converts integers < 4000 into roman numerals
  3. %
  4. %   Sample program for the T Interpreter by:
  5. %
  6. %   Stephen R. Schmitt
  7. %   962 Depot Road
  8. %   Boxborough, MA 01719
  9. %
  10.  
  11. const wh : string := "IVXLCDM"
  12.  
  13. program
  14.  
  15.     var in : int
  16.  
  17.     loop
  18.  
  19.         prompt "integer ( <1 to stop ):"
  20.         get in
  21.  
  22.         exit when in < 1
  23.  
  24.         put in, " = ", roman( in )
  25.  
  26.     end loop
  27.  
  28. end program
  29.  
  30.  
  31. %
  32. %   convert from integer to roman numeral
  33. %
  34. function roman( in : int ) : string
  35.  
  36.     label roman_exit:
  37.     var number, divisor : int
  38.     var i, j, k, p : int
  39.     var str : string
  40.  
  41.     number := in
  42.     divisor := 1000
  43.     p := 0
  44.  
  45.     if number >= 4000 then
  46.  
  47.         str := "Too big, re-enter"
  48.         goto roman_exit
  49.  
  50.     end if
  51.  
  52.     for decreasing k := 3 ... 0 do
  53.         
  54.         i := number div divisor
  55.         number := number mod divisor
  56.  
  57.         case i of
  58.  
  59.             value 0:
  60.                 % do nothing
  61.  
  62.             value 5:
  63.                 str[p] := wh[2*k+1]
  64.                 p := p + 1
  65.  
  66.             value 9:
  67.                 str[p] := wh[2*k]
  68.                 p := p + 1
  69.                 str[p] := wh[2*k+2]
  70.                 p := p + 1
  71.  
  72.             value 4:
  73.                 str[p] := wh[2*k]
  74.                 p := p + 1
  75.                 str[p] := wh[2*k+1]
  76.                 p := p + 1
  77.  
  78.             value : 
  79.                 if i > 5 then
  80.                     str[p] := wh[2*k+1]
  81.                     p := p + 1
  82.                     i := i - 5
  83.                 end if
  84.                 for j := 0 ... i-1 do
  85.                     str[p] := wh[2*k]
  86.                     p := p + 1
  87.                 end for
  88.  
  89.         end case
  90.  
  91.         divisor := divisor div 10
  92.  
  93.     end for
  94.  
  95.     str[p] := chr( 0 )
  96.  
  97.     roman_exit:
  98.     return str
  99.  
  100. end function